// pages/edit/edit.js





var ctx
var img={src:'',scale:1,width:''}
var x
var y
var X=0
var Y=0
var point={}
var dif
var dif1
var point1={}
var timeer
var timeer1
var sca
var devincewidth
var devinceheight



Page({
  data: {canvasHeight:'70%',canvasWidth:'90%'
    
  },
  onLoad: function(options) {
    console.log(options);
    
    img.src=options.url
    console.log(img.src);
    
    this.getmedia()
    this.getphoto(options.url)
    console.log(img.scale,img.width,img.height,devincewidth);
  },
  onReady: function() {
     console.log(img.scale,img.width,img.height,devincewidth);
     ctx=wx.createCanvasContext('myCanvas', this);
     if(img.bi>=1){
      console.log("宽大");
     if(img.width>=devincewidth*0.9){
       console.log("kuan>屏幕");
       img.width=devincewidth*0.9
       img.height=devincewidth*0.9/img.bi
     }
    }else{
      console.log("高大");
      if(img.height>=devinceheight*0.9){
        console.log("高大于屏幕");
        img.height=devinceheight*0.9
        img.width=devinceheight*0.9*img.bi
      }
    }
    
   console.log(ctx,img.width);
   
   ctx.drawImage(img.src, 0, 0, img.width*img.scale, 
     img.height*img.scale);
   ctx.draw()
  
    
    
  },
  getphoto(url){
    wx.getImageInfo({
      src: img.src,
      success: function(res) {
        img.width=res.width
        img.height=res.height
        img.bi=res.width/res.height
        console.log(img.bi,img.width);



       
        
      }
    })
  },
  start(e){
    if(e.touches.length==2){
    point.x1=e.touches[0].x
    point.x2=e.touches[1].x
    let xx1=point.x2-point.x1
    point.y1=e.touches[0].y
    point.y2=e.touches[1].y
    let yy1=point.y2-point.y1
     dif=Math.sqrt(xx1*xx1+yy1*yy1)}
    
     
         
else if(e.touches.length==1){
    
    console.log(e);
   x= e.changedTouches[0].x
    y=e.changedTouches[0].y}
  },
  move(e){
    if(e.changedTouches.length==2){
     

      point1.x1=e.changedTouches[0].x
      point1.x2=e.changedTouches[1].x
      let xx2=point1.x2-point1.x1
      point1.y1=e.changedTouches[0].y
      point1.y2=e.changedTouches[1].y
      let yy2=point1.y2-point1.y1
       dif1=Math.sqrt(xx2*xx2+yy2*yy2)
       let diff=dif1-dif
       console.log(dif,dif1,diff);
           if(diff>=0){
          sca*=1.001
     
         img.scale=img.scale+0.000005*diff
          if(img.scale>=1.5){
            img.scale=1.5
            console.log(img.scale,111111111111);
          }
          
          ctx.translate(X,Y)
          ctx.drawImage(img.src, 0, 0, img.width*img.scale, 
            img.height*img.scale);
           ctx.draw()
         
           
         
         
        
       }else{
       

          img.scale=img.scale+0.000005*diff
          if(img.scale<=0.001){
  
          img.scale=0.001
          console.log(img.scale,222222222222222222);
       }
       ctx.translate(X,Y)
       ctx.drawImage(img.src, 0, 0, img.width*img.scale, 
        img.height*img.scale);
       ctx.draw()
       
       
         
        }

    }else{
      let x1= e.changedTouches[0].x-x
      let y1=e.changedTouches[0].y-y
      X+=x1*0.01
      Y+=y1*0.01
      
      
   
      
        ctx.translate(X,Y)
        ctx.drawImage(img.src, 0, 0, img.width*img.scale, 
          img.height*img.scale);
         ctx.draw()
         
      
     
       


      }
   
  },
  saveclick(){
    console.log(ctx);
    wx.canvasToTempFilePath({
     
      canvasId: 'myCanvas',
      success:(res)=> {
        console.log(res.tempFilePath)
        var pages = getCurrentPages()   
    　　var currentPage = pages[pages.length - 1] 
    　　var prevPage = pages[pages.length - 2]    
    　　
    　　prevPage.setData({
    　　　　file:res.tempFilePath ,
  url:res.tempFilePath   })
         wx.navigateBack({
           complete: (res) => {},
         })
      }
    })
    
  },
  getmedia(){
    wx.getSystemInfo({
      success: function(res) {
        console.log(res);
        devincewidth=res.windowWidth
       devinceheight=res.windowHeight
    }
    })
  }
  
})




















/*var  ctx = ""
var img={}
Page({

  data:{
    canvasHeight: '85%',
    canvasWidth: '85%',x:'',y:'',bi:'',distance:'',url:'',dif:0
},
onLoad(options){
    this.setData({
      url:options.url
    })
},

onReady: function() {
    ctx = wx.createCanvasContext('myCanvas')
    img.src=this.data.url
    img.imgx=0
    img.imgy=0
    img.width=200
    img.height=200
    img.scale=1
    ctx.drawImage(img.src,img.imgx,img.imgy,img.scale*img.width,
      img.scale*img.height)
    ctx.draw()
    
  
   
  },
  start(e){
    if(e.changedTouches.length==2){
      var xp=  e.changedTouches[1].x-e.changedTouches[0].x
      var yp=e.changedTouches[1].y-e.changedTouches[0].y
      var dif=Math.sqrt(xp*xp+yp*yp)
      this.setData({dif})
      
      }else{
    console.log(e);
   var x= e.changedTouches[0].x
   var y=e.changedTouches[0].y
   this.setData({x,y})}
  },
  move(e){
    if(e.changedTouches.length==2){
      var xp=  e.changedTouches[1].x-e.changedTouches[0].x
      var yp=e.changedTouches[1].y-e.changedTouches[0].y
      var dif=Math.sqrt(xp*xp+yp*yp)
      var scale=(dif-this.data.dif)*0.5
      if(scale>=2){scale=2
      img.scale=scale
      setTimeout(()=>{
        ctx.drawImage(img.src,img.imgx,img.imgy,img.scale*img.width,
          img.scale*img.height)
     ctx.draw()
      },500)
      }else if(scale<=0.6){
        scale=0.6
        setTimeout(()=>{
          ctx.drawImage(img.src,img.imgx,img.imgy,img.scale*img.width,
            img.scale*img.height)
       ctx.draw()
        },500)
      }
      
   
      }else{
    console.log(e);
    var x= e.changedTouches[0].x
   var y=e.changedTouches[0].y
   var dx=x-this.data.x
   var dy=y-this.data.y
   img.imgx=dx
   img.imgy=dy
   
   setTimeout(()=>{
    ctx.drawImage(img.src,img.imgx,img.imgy,img.scale*img.width,
      img.scale*img.height)
 ctx.draw()
  },500)
  }
  
  }
  

  
  
})*/